package com.longrise.android.byjk.plugins.poster.posterdetail;

import android.os.Handler;

import com.longrise.LEAP.Base.Objects.EntityBean;
import com.longrise.android.byjk.common.UrlConstants;
import com.longrise.android.byjk.event.PosterEvent;
import com.longrise.android.byjk.plugins.poster.bean.ImageBean;
import com.longrise.android.byjk.plugins.poster.posterdetail.assist.AccessCount;
import com.longrise.android.byjk.plugins.poster.posterdetail.assist.ImgList;
import com.longrise.android.loaddata.newloaddata.LoadDataManagerFather;
import com.longrise.common.base.BasePresenter;
import com.longrise.common.datasource.remote.LoadDataManager;
import com.longrise.common.utils.PrintLog;
import com.longrise.umeng.share.PosterShare;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;


/**
 * Created by godliness on 2017/9/25.
 * 海报分享页面
 */

public class PosterDetailPresenter extends BasePresenter<PosterDetailView> {

    private static final String TAG = "PosterDetailPresenter";

    private final int next = 1;
    private final int prve = 0;

    private Handler mHandler = new Handler();

    private boolean mFinished;

    public void setFinished() {
        mFinished = true;
    }

    private List<ImageBean> imageList = new ArrayList<>();


    @Override
    protected void init() {
    }

    /**
     * 通知posterfragment 把需要存到本地的 图片 存起来
     */
    public void preformShare(int flag, String id) {
        PosterEvent event  = new PosterEvent();
        PrintLog.e("PoseterDetailPresenter","event=:"+event.toString());
        event.setSavePic(true);
        event.setFlag(flag);
        event.setId(id);
        EventBus.getDefault().post(event);
        PrintLog.e("PoseterDetailPresenter","1111111111");
    }

    /**
     * 执行分享
     */
    public void toShare(int flag, String id, PosterFragPresenter.CreateSave createSave) {
        if (createSave != null && createSave.mSaved) {
            sharePoster(flag, createSave.mSavePath, id);
        }
    }

    /**
     * 下一张
     */
    public void next(int currentPosition, int totalSize) {
        currentPosition++;
        if (currentPosition >= totalSize) {
            currentPosition = totalSize - next;
        }
        mView.setCurrentPosition(currentPosition);
    }

    /**
     * 上一张
     */
    public void prev(int currentPosition) {
        currentPosition--;
        if (currentPosition < prve) {
            currentPosition = prve;
        }
        mView.setCurrentPosition(currentPosition);
    }



    public void updateReadStatus(EntityBean bean) {
        EntityBean fbean = new EntityBean();
        fbean.set("bean", bean);
        LoadDataManager.getInstance().callService2(null, UrlConstants.BaseUrl, "bbt_vip_uPostersInfoByViewnum", fbean, new LoadDataManager.OnRequestCallListener2() {

            @Override
            public void onSuccess(String key, String service, Object result) {
                try {
                    EntityBean bean = (EntityBean) result;
                    String resultdesc = bean.getString("resultdesc");
                    int value = (Integer) bean.get("resultstate");
                    if (value == 1) {
                        AccessCount.getInstance().removeReadAll();
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onError(String key, String service, LoadDataManagerFather.ResultType resultType) {

            }

            @Override
            public void onFinished(String key, String service) {
            }

        });

    }


    public void updateShareStatus(EntityBean bean) {
        EntityBean fbean = new EntityBean();
        fbean.set("bean", bean);
        PrintLog.e("details share bean", " value = " + bean);
        LoadDataManager.getInstance().callService2(null, UrlConstants.BaseUrl, "bbt_vip_uPostersInfoBySharenum", fbean, new LoadDataManager.OnRequestCallListener2() {

            @Override
            public void onSuccess(String key, String service, Object result) {
                try {
                    EntityBean bean = (EntityBean) result;
                    String resultdesc = bean.getString("resultdesc");
                    int value = (Integer) bean.get("resultstate");
                    if (value == 1) {
                        AccessCount.getInstance().removeShareAll();
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onError(String key, String service, LoadDataManagerFather.ResultType resultType) {

            }

            @Override
            public void onFinished(String key, String service) {
            }

        });

    }

    /**
     * 获取展示的数据
     */
    public void getData(String type) {
        EntityBean bean = new EntityBean();
        bean.set("goodstype", type);
        bean.set("pagenum", "-1");
        bean.set("pagesize", 0);
        EntityBean fbean = new EntityBean();
        fbean.set("bean", bean);
        LoadDataManager.getInstance().callService2(null, UrlConstants.BaseUrl, "bbt_vip_sPostersInfoBygoodstype", fbean, new LoadDataManager.OnRequestCallListener2() {

            @Override
            public void onSuccess(String key, String service, Object result) {
                try {
                    EntityBean bean = (EntityBean) result;
                    parseResult(bean);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onError(String key, String service, LoadDataManagerFather.ResultType resultType) {

            }

            @Override
            public void onFinished(String key, String service) {
            }

        });
    }

    private void parseResult(EntityBean bean) {
        ImgList.getInstance().clearAll();
        String resultdesc = bean.getString("resultdesc");
        int value = (Integer) bean.get("resultstate");
        if (value == 1) {
            EntityBean[] beans = bean.getBeans("result");
            for (int i = 0; i < beans.length; i++) {
                String goodsid = beans[i].getString("id");
                String backpicurl = beans[i].getString("backpicurl");
                String showpicurl = beans[i].getString("showpicurl");
                String detailpicurl = beans[i].getString("detailpicurl");
                String needscore = beans[i].getString("needscore");
                String areaname = beans[i].getString("areaname");
                String rankvalue = beans[i].getString("rankvalue");
                String showsharenum = beans[i].getString("showsharenum");
                String isnew = beans[i].getString("isnew");
                ImageBean imageBean = new ImageBean();
                imageBean.setAreaname(areaname);
                imageBean.setGoodsid(goodsid);
                imageBean.setBackpicurl(backpicurl);
                imageBean.setDetailpicurl(detailpicurl);
                imageBean.setShowsharenum(showsharenum);
                imageBean.setIsnew(isnew);
                imageBean.setRankvalue(rankvalue);
                imageBean.setNeedscore(needscore);
                imageBean.setShowpicurl(showpicurl);
                imageList.add(imageBean);
            }
            ImgList.getInstance().addAll(imageList);
            mView.finishGetImage(true);
        }
    }

    /**
     * 执行分享的方法
     * @param flag   区分不同分享平台 1、QQ空间，2、微信好友，3、微信朋友圈，4、QQ好友
     * @param savePath   图片的存储地址
     */
    private void sharePoster(int flag, String savePath, final String id) {
        PosterShare posterShare = new PosterShare(mContext);
        PrintLog.e(TAG,"savePath=:"+savePath.toString());
        posterShare.share(flag,savePath,id);
        posterShare.setDialogCallback(new PosterShare.ShareCallback() {
            @Override
            public void shareSuccess() {
                mHandler.post(new Runnable() {
                    @Override
                    public void run() {
                        AccessCount.getInstance().accessShareIn(id);
                        mView.actFinish(0, id);
                        PrintLog.e(TAG,"--------shareSuccess--------");
                    }
                });
            }

            @Override
            public void shareFailed() {
                mHandler.post(new Runnable() {
                    @Override
                    public void run() {
                        mView.actFinish(1, id);
                        PrintLog.e(TAG,"--------shareFailed--------");
                    }
                });
            }

            @Override
            public void shareCancel() {
                mHandler.post(new Runnable() {
                    @Override
                    public void run() {
                        mView.actFinish(2, id);
                        PrintLog.e(TAG,"--------shareCancel--------");
                    }
                });
            }
        });
    }
}
